using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class POrderDetailDAL
    {
        public static int Save(POrderDetailInfo info)
        {
            string sql = string.Format("insert into [porderdetail]([AdminID],[UserID],[OrderID],[ProductID],[Name],[OPrice],[APrice],[PPrice],[Unit],[Img],[Count],[Format],[CreateDate]) values(@AdminID,@UserID,@OrderID,@ProductID,@Name,@OPrice,@APrice,@PPrice,@Unit,@Img,@Count,@Format,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@OrderID", info.OrderID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@PPrice", info.PPrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@Format", info.Format),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(POrderDetailInfo info)
        {
            string sql = string.Format("update [porderdetail] set [AdminID]=@AdminID,[ProductID]=@ProductID,[Name]=@Name,[OPrice]=@OPrice,[APrice]=@APrice,[PPrice]=@PPrice,[Unit]=@Unit,[Img]=@Img,[Count]=@Count,[Format]=@Format where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@PPrice", info.PPrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@Format", info.Format),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static POrderDetailInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[porderdetail]");
            return SimpleData(dr);
        }
        public static List<POrderDetailInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[porderdetail]");
            return ListSimpleData(dt);
        }
        public static List<POrderDetailInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [porderdetail].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [porderdetail].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[porderdetail]", out count);
            return ListSimpleData(dt);
        }
        public static List<POrderDetailInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [porderdetail]"));
            return ListSimpleData(dt);
        }
        public static List<POrderDetailInfo> FindListByOrderID(int adminid,int userid, int orderid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [porderdetail] where AdminID={0} and UserID={1} and OrderID={2}",adminid,userid,orderid));
            return ListSimpleData(dt);
        }
        public static List<POrderDetailInfo> FindListByOrderIDs(int adminid, string orderids)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [porderdetail] where AdminID={0} and OrderID in ({1})",adminid, orderids));
            return ListSimpleData(dt);
        }
        private static POrderDetailInfo SimpleData(DataRow dr)
        {
            POrderDetailInfo info = new POrderDetailInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static POrderDetailInfo AllData(DataRow dr)
        {
            POrderDetailInfo info = new POrderDetailInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<POrderDetailInfo> ListSimpleData(DataTable dt)
        {
            List<POrderDetailInfo> list = new List<POrderDetailInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<POrderDetailInfo> ListAllData(DataTable dt)
        {
            List<POrderDetailInfo> list = new List<POrderDetailInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

